<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

    //系统首页
    public function index(){
        if (ismobile()){
            $this->redirect("/Wap/Student/index");
        }else{
            $this->display();
        }
    }

    public function login()
    {
        //获取登录信息
        if (IS_POST) {
            $username=I('post.username');
            $password=I('post.password');
            $uid = D('Agent')->login($username, $password);
            if(0 < $uid){ //UC登录成功
                $this->success('登录成功！', U('Index/agent'));
            } else { //登录失败
                if($uid==0){
                    $this->error('用户正在审核！', '', true);
                }else if($uid==-3){
                    $this->error('用户已被禁用！', '', true);
                }else{
                    $this->error('用户名或密码不正确！', '', true);
                }
            }

        }else{
            if(is_login()){
                $this->redirect('Index/agent');
            }else{
                $this->display();
            }
        }

    }

    //注册
    public function register(){
        //获取登录信息
        if (IS_POST) {
            $data['username']=I('post.username');
            $data['password']=I('post.password');
            $repassword=I('post.repassword');
            $data['nickname']=I('post.nickname');
            $data['mobile']=I('post.mobile');
            $data['department']=I('post.department');
            $re=M('Agent')->where(array('mobile'=>$data['mobile']))->find();
            if(is_array($re)){
                $this->error('手机已注册');
            }
            /* 检测密码 */
            if( $data['password'] != $repassword){
                $this->error('密码和重复密码不一致！');
            }
            $uid    =   D('Agent')->register($data);
            if(0 < $uid){ //注册成功
                $this->success('注册成功！',U('login'));
            } else { //注册失败，显示错误信息
                $this->error($this->showRegError($uid));
            }
        }else{
            $department_info=M('department_info')->field('id,title')->select();
            $this->assign('department_info',$department_info);
            $this->display();
        }
    }


    /**
     * 代理用户
     */
    public function agent(){
        if(!is_login()){
            $this->error('请先登录',U('login'));
        }
        $major=M('major')->field('id,title')->select();
        $this->assign('data',null);
        $this->assign('major',$major);
        $this->display();
    }

    public function editSudentInfo(){
        if(!is_login()){
            $this->error('请先登录',U('login'));
        }
        $id=I('get.id');
        $data=M('student_info')->where(array('id'=>$id))->find();
        if($data['pay_evidence']!=''){
            $this->error('不能编辑',U('sudentManage'));
        }
        $major=M('major')->field('id,title')->select();
        $this->assign('major',$major);
        $this->assign('data',$data);
        $this->display('agent');
    }

    public function showStudentInfo(){
        if(!is_login()){
            $this->error('请先登录',U('login'));
        }
        $id=I('get.id');
        $data=M('student_info')->where(array('id'=>$id))->find();
        $this->assign('data',$data);
        $this->display();
    }


    public function checkfield(){
        $where['ID_number']=I('post.ID_number');
        $data=M('student_info')->where($where)->find();
        if(is_array($data)){
            $this->ajaxReturn(['status'=>1,"info"=>'身份证号已存在']);
        }else{
            $this->ajaxReturn(['status'=>0]);
        }
    }

    public function saveSudentInfo(){
        $uid=is_login();
        if(!$uid){
            $this->error('请先登录',U('login'));
        }
        if(IS_POST){
            $data=I('post.');
            $data['upload_time']=empty($data['pay_evidence'])?'':time();
            $data['type']=2;
            $data['introducer_id']=$uid;
            $student=M('student_info')->where(array('ID_number'=>$data['ID_number']));
            if(empty($student)){ //新增数据
                $data['create_time']=time();
                $res = M('student_info')->add($data);
                if(!$res){
                    $this->error ("添加出错");
                }else{
                    $this->success ('添加成功！', U('sudentManage') );
                }
            } else { //更新数据
                $data['update_time']=time();
                $status = M('student_info')->save($data);
                if(false === $status){
                    $this->error ("更新出错");
                }else{
                    $this->success ('更新成功！', U('sudentManage') );
                }
            }
        }
    }


    public function sudentManage($search_word=null){
        $map['introducer_id'] = is_login();
        if(!$map['introducer_id']){
            $this->error('请先登录',U('login'));
        }
        $status=I('get.status');
        if($status!='' && $status!=null){
            $map['status']=$status;
        }else{
            $map['status']=array('gt',-1);
        }
        /* 查询条件初始化 */
        if(isset($search_word)){
            $map['name|ID_number']=   array(array('like','%'.$search_word.'%'),array('like','%'.$search_word.'%'),'_multi'=>true);
        }
        $total = NULL;
        $model=M('student_info');
        $page = getpage($model, $map, 12, $total);
        $info = $model->where($map)->field("id,name,ID_number,pay_evidence,introducer,create_time,upload_time,status")->select();
        $this->assign('info',$info);
        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->display();

    }


    /**
     * 学生端
     */
    public function student(){
        $major=M('major')->field('id,title')->select();
        $this->assign('major',$major);
        $this->display();
    }

    /**
     * 保存数据
     */
    public function saveData(){
        if(IS_POST){
            $data=I('post.');
            $data['type']=1;
            $data['create_time']=time();
            if(empty($data['id'])){ //新增数据
                $data['create_time']=time();
                $res = M('student_info')->add($data);
                if(!$res){
                    $this->error ("提交出错");
                }else{
                    $this->success ('提交成功！');
                }
            } else { //更新数据
                $data['update_time']=time();
                $res = M('student_info')->save($data);
                if(!$res){
                    $this->error ("提交出错");
                }else{
                    $this->success ('提交成功！');
                }
            }

        }
    }

    //学生入学报道
    public  function studentReport(){
        if(IS_POST){
            $data=I('post.');
            $data['type']=1;
            $data['create_time']=time();
            $data['is_report']=1;
            if(empty($data['id'])){ //新增数据
                $data['create_time']=time();
                $res = M('student_info')->add($data);
                if(!$res){
                    $this->error ("提交出错");
                }else{
                    $this->success ('提交成功！');
                }
            } else { //更新数据
                $data['update_time']=time();
                $res = M('student_info')->save($data);
                if(!$res){
                    $this->error ("提交出错");
                }else{
                    $this->success ('提交成功！');
                }
            }

        }else{
            $major=M('major')->field('id,title')->select();
            $this->assign('major',$major);
            $class=M('class')->field('id,title')->select();
            $this->assign('class',$class);
            $this->display();
        }
    }

    //获取学生信息
    public function getStudentInfo(){
        if(IS_POST){
            I('post.name')?$where['name']=I('post.name'):'';
            $ID_number=I('post.ID_number');
            if($ID_number){
                if(count($ID_number)>4){
                    $where['ID_number']= $ID_number;
                }else{
                    $where['ID_number']=array('like','%'.$ID_number);
                }
            }

            if(isset($where)){
                $student=M('student_info')
                    ->where($where)
                    ->field('id,name,ID_number,mobile,home_address,class_id,school,major')
                    ->find();
                $this->ajaxReturn(['status'=>1,'info'=>$student]);
            }else{
                $this->error ("数据错误");
            }
        }
    }

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            session('user_auth', null);
            session('user_auth_sign', null);
            $this->redirect('Index/login');
        } else {
            $this->redirect('Index/login');
        }
    }

}